package com.uphone.magicleafteacher.pro.fragment.mine;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.google.gson.Gson;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.entity.LocalMedia;
import com.radish.baselibrary.dialog.RadishDialog;
import com.radish.baselibrary.utils.ActivityCollector;
import com.radish.framelibrary.view.glide.GlideUtils;
import com.uphone.magicleafteacher.MyApp;
import com.uphone.magicleafteacher.R;
import com.uphone.magicleafteacher.base.OnPermissionCallBack;
import com.uphone.magicleafteacher.base.mvp.BaseMvpFragment;
import com.uphone.magicleafteacher.http.ApiService;
import com.uphone.magicleafteacher.http.RxSchedulers;
import com.uphone.magicleafteacher.pro.activity.StudentDetailsActivity;
import com.uphone.magicleafteacher.pro.activity.mine.MainpasdActivity;
import com.uphone.magicleafteacher.pro.activity.mine.MineaboutActivity;
import com.uphone.magicleafteacher.pro.activity.mine.MinefeedActivity;
import com.uphone.magicleafteacher.pro.activity.welcome.WelcomeActivity;
import com.uphone.magicleafteacher.util.CommonUtil;
import com.uphone.magicleafteacher.util.DialogUtils;
import com.uphone.magicleafteacher.util.GsonUtils;
import com.uphone.magicleafteacher.util.PictureSelectUtils;
import com.uphone.magicleafteacher.vo.TeacherBean;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 说明：我的
 * 2019/11/5 15:53
 */
public class MineFragment extends BaseMvpFragment<MinePeresenter> implements MineContract.View {
    @BindView(R.id.iv_head)
    ImageView ivHead;
    @BindView(R.id.text_teachwe_name)
    TextView textTeachweName;
    @BindView(R.id.text_teacher_age)
    TextView textTeacherAge;
    @BindView(R.id.lv_information)
    LinearLayout lvInformation;
    @BindView(R.id.ll_mine_psd)
    LinearLayout llMinePsd;
    @BindView(R.id.ll_mine_feedback)
    LinearLayout llMineFeedback;
    @BindView(R.id.ll_mine_about)
    LinearLayout llMineAbout;
    @BindView(R.id.btn_submit)
    Button btnSubmit;
    @BindView(R.id.iv_xing)
    ImageView ivXing;

    public static MineFragment newInstance(String title) {

        Bundle args = new Bundle();

        MineFragment fragment = new MineFragment();

        return fragment;
    }

    @Override
    protected void initInject() {
        getFragmentComponent().inject(this);
    }

    @Override
    protected void initBundle() {

    }

    @Override
    protected int initLayout() {
        return R.layout.fragment_mine;
    }

    @SuppressLint("CheckResult")
    @Override
    protected void initView() {
        SharedPreferences preferences = getActivity().getSharedPreferences("moye", Activity.MODE_PRIVATE);
        String token = preferences.getString("token", "");
        String teacherId = preferences.getString("teacherId", "");
        Map<String, Object> map = new HashMap<>();
        map.put("token", token);
        Map<String, Object> map1 = new HashMap<>();
        map1.put("teacherId", teacherId);
        MyApp.apiService(ApiService.class)
                .infoteacher(map, map1)
                .compose(RxSchedulers.io_main())
                .subscribe(bean -> {
                    if (getActivity() != null)
//                        .closeLoading();
                        if (bean != null) {
                            try {
                                String jsonStr = bean.string();
                                Gson gson = GsonUtils.getGson();
                                TeacherBean teacherBean = gson.fromJson(jsonStr, TeacherBean.class);

                                textTeachweName.setText(teacherBean.getTeacherInfo().getTeacherName() + "");
                                textTeacherAge.setText(teacherBean.getTeacherInfo().getAge() + "");
                                GlideUtils.getInstance().loadCircleImage(getContext(),CommonUtil.getImageUrl(teacherBean.getTeacherInfo().getTeacherImg()),ivHead);
                                String gender = teacherBean.getTeacherInfo().getGender();
                                int i = Integer.parseInt(gender);
                                if (i == 1) {
                                    Glide.with(getActivity()).load(R.drawable.ic_man).into(ivXing);
                                } else {
                                    Glide.with(getActivity()).load(R.drawable.ic_woman).into(ivXing);
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                }, throwable -> {

//                    MinefeedActivity.this.closeLoading();
//                    MinefeedActivity.this.onFail();
                });

    }

    @Override
    protected void loadData() {

    }

    @Override
    protected void initListener() {

    }

    @OnClick({R.id.iv_head,R.id.ll_mine_psd, R.id.ll_mine_feedback, R.id.ll_mine_about, R.id.btn_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_head://更换头像
                selectPic();
                break;
            //修改密码
            case R.id.ll_mine_psd:
                Intent intent = new Intent(getActivity().getApplicationContext(), MainpasdActivity.class);
                startActivity(intent);
                break;
            //意见反馈
            case R.id.ll_mine_feedback:
                Intent intent1 = new Intent(getActivity().getApplicationContext(), MinefeedActivity.class);
                startActivity(intent1);
                break;
            //关于魔叶
            case R.id.ll_mine_about:
                Intent intent2 = new Intent(getActivity().getApplicationContext(), MineaboutActivity.class);
                startActivity(intent2);
                break;
            //退出
            case R.id.btn_submit:
                DialogUtils.alterDialog(getActivity(), "确认退出登录？", (dialog, view1, type) -> {
                    CommonUtil.startLogin();
                });
                break;
        }
    }


    private void selectPic() {
        getPermission(new OnPermissionCallBack() {
            @Override
            public void permissionPass(String[] permissions) {
                PictureSelectUtils.openMultiplePicture(getActivity(), new ArrayList<>()
                        , 1, false, 0, 0, PictureConfig.CHOOSE_REQUEST);
            }

            @Override
            public void permissionRefuse(String[] permissions) {

            }
        }, Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE);
    }

    private File picFile;
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == getActivity().RESULT_OK) {
            switch (requestCode) {
                case PictureConfig.CHOOSE_REQUEST:
                    // 图片、视频、音频选择结果回调
                    List<LocalMedia> selectList = PictureSelector.obtainMultipleResult(data);
                    if (selectList != null && selectList.size() > 0) {
                        String path = selectList.get(0).getPath();
                        if (!TextUtils.isEmpty(path) && new File(path).exists()) {
                            picFile = new File(path);
                            mPresenter.uploadImgFile(picFile);
                        }
                    }

                    break;
                default:
                    break;
            }
        }
    }

    @Override
    public void updateFile(String picUrl) {
        if (picFile != null && picFile.exists())
            GlideUtils.getInstance().loadCircleImage(getBaseActivity(), picFile, ivHead);
        else
            GlideUtils.getInstance().loadCircleImage(getBaseActivity(), CommonUtil.getImageUrl(picUrl), ivHead);
    }

    public void uploadImgFile(File file) {
        picFile = file;
        mPresenter.uploadImgFile(file);
    }
}
